#include "picshow.h"
#include "ui_picshow.h"
#include <QPixmap>
#include <QFileDialog>
#include <QDebug>

Picshow::Picshow(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::Picshow)
{
    ui->setupUi(this);
    Init();
}

Picshow::~Picshow()
{
    delete ui;
}

void Picshow::Init()
{
    //设置窗体大小
    this->resize(800, 480);
    this->move(0, 0);

    //申请定时器内存
    my_time = new QTimer(this);
    connect(my_time,SIGNAL(timeout()),this,SLOT(timeout_show_photo()));

    //设置主体颜色
    QColor c(249, 251, 251);
    QPalette p;
    p.setColor(QPalette::Window,c); //设置背景颜色
    this->setPalette(p);

    ui->label->setPalette(p);
    ui->scrollArea->setPalette(p);
    this->num = 0;

    //初始化图片大小
    this->photo_w = ui->label->width();
    this->photo_h = ui->label->height();

    tmp_filename = "";
    filename = "";
    dirlist.clear();
    photo_rotating = 0;
    ui->label->setText("请选择图片");
    //设定文件夹路径
//    filedir = QString(tr("C:/Users/Administrator/Desktop/test"));
    filedir = QString(tr("/IOT/myqt/photo"));
    Init_photo_list();
}

void Picshow::Init_photo_list()
{
    QFileInfo fi;
    QDir dir(filedir);
    dirlist<<filedir;
    QStringList nameFilters, file_list;
    if (dir.exists())   //判断目录是否存在
    {
        nameFilters << "*.jpg" << "*.png"<<".bmp"<<".JPEG";
        file_list = dir.entryList(nameFilters,QDir::Files | QDir::NoDotAndDotDot);
        qDebug() << file_list;
        if(file_list.count()!=0){
            //初始化文件列表
//            this->photo_list.clear();
            for (int i =0; i<file_list.count(); i++) {
                photo_list<<filedir + "/" + file_list[i];
            }
            //将文件名挨个加入选择列表
            for (int i =0; i<file_list.count(); i++) {
                ui->listWidget->addItem(file_list[i]);
            }
            //初始化当前需要播放的文件名
            if(filename == NULL)
            {
                filename = photo_list[0];
            }
            this->num = photo_list.indexOf(filename) + 1;
            show_photo_thing();
        }
    }
    else{
    }
}

//图片缩小
void Picshow::on_btn_short_clicked()
{
    my_time->stop();
    if(this->num){
        QPixmap pix(filename);
        this->photo_w = this->photo_w*(1-0.2);
        this->photo_h = this->photo_h*(1-0.2);
        pix = pix.scaled(this->photo_w, this->photo_h, \
                         Qt::KeepAspectRatio, Qt::SmoothTransformation);  // 按比例缩放
        ui->label->setPixmap(pix);
    }
    else{
        ui->label->setText("请选择照片");
    }

}
//图片放大
void Picshow::on_btn_big_clicked()
{
    my_time->stop();
    if(this->num){
        QPixmap pix(filename);
        this->photo_w = this->photo_w*(1+0.2);
        this->photo_h = this->photo_h*(1+0.2);
        pix = pix.scaled(this->photo_w, this->photo_h, \
                         Qt::KeepAspectRatio, Qt::SmoothTransformation);  // 按比例缩放
        ui->label->setPixmap(pix);
    }
    else{
        ui->label->setText("请选择照片");
    }
}

//void Picshow::find_photo_int_current_dir()
//{
//    QFileInfo fi = QFileInfo(filename);

//    QString file_name, file_path;
//    file_name = fi.fileName();  //获取文件名

//    file_path = fi.absolutePath(); //获取文件路径(不包含文件名)
//    qDebug()<<filename<<" "<<file_path<<" "<<file_name;


//    QDir dir(file_path);

//    QStringList nameFilters, file_list;
//    if (dir.exists())   //判断目录是否存在
//    {
//        nameFilters << "*.jpg" << "*.png";
//        file_list = dir.entryList(nameFilters,QDir::Files | QDir::NoDotAndDotDot);
//        qDebug() << file_list;
//        this->photo_list.clear();
//        for (int i =0; i<file_list.count(); i++) {
//            photo_list<<file_path + "/" + file_list[i];
//        }
//        this->num = photo_list.indexOf(filename) + 1;
//    }
//    else{
//        qDebug() << "该目录不存在！！！";
//    }
//}
//选择图片
void Picshow::on_change_path_clicked()
{
    my_time->stop();
    filename=QFileDialog::getOpenFileName(this,
                                          tr("选择图像"),
                                          "",
                                          tr("Images (*.png *.bmp *.jpg *.tif *.GIF )"));
    if(filename.isEmpty())
    {
         return;
    }
    else
    {
        QFileInfo fi = QFileInfo(filename);
        filedir = fi.absolutePath(); //获取文件路径(不包含文件名)
        if((dirlist.contains(filedir)) || (this->num!=0)){
        }
        else{
            Init_photo_list();
        }
    }
}
//上一张
void Picshow::on_last_photo_clicked()
{
    my_time->stop();
    if(this->num)
    {
        if(this->num != 1){
            this->num -= 1;
            filename = photo_list[this->num - 1];
            show_photo_thing();
        }
        else{
            this->num = photo_list.count();
            filename = photo_list[this->num - 1];
        }
    }
    else{
           ui->label->setText("请选择照片");
    }
}

//下一张
void Picshow::on_next_photo_clicked()
{
    my_time->stop();
    if(this->num)
    {
        if(this->num != photo_list.count()){
            this->num += 1;
            filename = photo_list[this->num - 1];
            show_photo_thing();
        }
        else{
            this->num = 1;
            filename = photo_list[this->num - 1];
            show_photo_thing();
        }
    }
    else{
           ui->label->setText("请选择照片");
    }
}
//左转
void Picshow::on_Right_rotation_clicked()
{
    my_time->stop();
    image.load(filename);
    if(tmp_filename == filename)
     {
            photo_rotating += 90;
            QMatrix matrix;
            matrix.rotate(photo_rotating);
            image = image.transformed(matrix,Qt::FastTransformation);
            ui->label->setPixmap(QPixmap::fromImage(image));
        }
    else{
        tmp_filename = filename;
        photo_rotating = 90;
        QMatrix matrix;
        matrix.rotate(photo_rotating);
        image = image.transformed(matrix,Qt::FastTransformation);
        ui->label->setPixmap(QPixmap::fromImage(image));
    }
}
//右转
void Picshow::on_Left_rotation_clicked()
{
    my_time->stop();
    image.load(filename);
    if(tmp_filename == filename)
     {
            photo_rotating -= 90;
            QMatrix matrix;
            matrix.rotate(photo_rotating);
            image = image.transformed(matrix,Qt::FastTransformation);
            ui->label->setPixmap(QPixmap::fromImage(image));
     }
    else{
        tmp_filename = filename;
        photo_rotating = -90;
        QMatrix matrix;
        matrix.rotate(photo_rotating);
        image = image.transformed(matrix,Qt::FastTransformation);
        ui->label->setPixmap(QPixmap::fromImage(image));
    }
}
//返回主页面
void Picshow::on_Return_to_main_menu_clicked()
{
       this->done(0);
}
//返回主页面
void Picshow::on_btn_close_exit_clicked()
{
    this->done(0);
}

//点击浏览图片
void Picshow::on_listWidget_itemClicked(QListWidgetItem *item)
{
    QFileInfo fi = QFileInfo(filename);

    QString file_name;
    file_name = fi.fileName();  //获取文件名

    if(item->text() != file_name){

        filename = filedir +"/" + item->text();
        this->num = photo_list.indexOf(filename) + 1;
        show_photo_thing();
    }
}

//自动播放
void Picshow::on_btn_auto_to_show_clicked()
{
    static qint8 auto_static = 1;
    if(this->num){
        if(auto_static){
            my_time->start(1000);
            auto_static = 0;
        }
        else{
            my_time->stop();
            auto_static = 1;
        }
    }
}
void Picshow::timeout_show_photo()
{
    if(this->num)
    {
        if(this->num != photo_list.count())
        {
            this->num += 1;
            filename = photo_list[this->num - 1];
        }
        else{
            this->num = 1;
            filename = photo_list[this->num - 1];
        }
        show_photo_thing();
    }
    else
        ui->label->setText("请选择图片");
}

void Picshow::show_photo_thing()
{
    QPixmap pix(filename);
    this->photo_w = ui->label->width();
    this->photo_h = ui->label->height();

    pix = pix.scaled(this->photo_w, this->photo_h, \
                     Qt::KeepAspectRatio, Qt::SmoothTransformation);  // 按比例缩放
    ui->label->setPixmap(pix);
    ui->listWidget->setCurrentRow(this->num-1);
}
